import { config } from "../config/config";
let time=0;
const quest = (params) => {
    let token = wx.getStorageSync('token')
    // console.log("Bearer "+token);
    time++
    wx.showLoading({
        title: '正在加载',
    })
    return new Promise((resolve, reject) => {
        wx.request({
            ...params,
            header: {
                Authorization:token
            },
            url: config.baseApi + params.url,
            success: (res) => {
                resolve(res.data)
            },
            fail: ( err) => {
                reject(err)
            },
            complete:()=>{
                time--
                if (time === 0) {
                  wx.hideLoading()
                }
            }
        })
    });
}


const login = () => {
    return new Promise((resolve, reject) => {
        wx.login({
            success: (res) => {
                // console.log(res);
                resolve(res.code)

            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}
const getUserProfile = () => {
    return new Promise((resolve, reject) => {
        wx.getUserProfile({
            desc: '展示用户信息',
            success: (res) => {
                resolve(res.userInfo)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}

const showModel = (params) => {
    return new Promise((resolve, reject) => {
         wx.showModal({
            title: params.title,
            content: params.content,
            success (res) {
              if (res.confirm) {
              resolve(res)
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
       
    
    })
}

export {
    quest ,login,getUserProfile,showModel
}